home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / system / from-0.000 / from-0 / from-0.5 / from < prev    next >
Encoding:
Text File  |  1995-08-03  |  3.0 KB  |  133 lines

  1. #!/usr/local/bin/perl
  2. # $Id: from,v 1.2 1995/08/03 08:51:30 sverrehu Exp $
  3. $id = $0;
  4. $id =~ s#.*/(.*)#$1#;
  5. #############################################################################
  6. ### from v0.5 [3/8/95] by Sverre H. Huseby, Norway.
  7. ###
  8. ### A program that will list sender and subject of unread mail (mail found
  9. ### in the system mailbox). Command line arguments are as for the BSD
  10. ### version.
  11. ###
  12. ### Configuration section ###################################################
  13.  
  14. $maildir = "/var/spool/mail";
  15.  
  16. ### End of configuration section ############################################
  17.  
  18. $user = `whoami`;
  19. chop $user;
  20. $mailcount = $longest_from = 0;
  21. @from = ();
  22. @subject = ();
  23. $arguser = $argfile = $argsender = "";
  24.  
  25. sub mimeToChar {
  26.     local($mstr) = @_;
  27.     $mstr =~ s/=//;
  28.     pack("C", hex($mstr));
  29. }
  30.  
  31. sub unMimeWord {
  32.     local($in) = @_;
  33.     $in =~ s/_/\x20/g;
  34.     $in =~ s/(=..)/&mimeToChar($1)/ige;
  35.     $in;
  36. }
  37.  
  38. sub unMime {
  39.     local($str) = @_;
  40.     $str =~ s/=\?iso-8859-1\?q\?([^\?]*)\?=/&unMimeWord($1)/ige;
  41.     $str;
  42. }
  43.  
  44. while (@ARGV) {
  45.     $arg = shift(@ARGV);
  46.     if ($arg =~ /^-(.)\s*(.*)/) {
  47.     ($first, $rest) = ($1, $2);
  48.     if ($first eq "s") {
  49.         if ($rest) {
  50.         $argsender = $rest;
  51.         } else {
  52.         $argsender = shift(@ARGV);
  53.         die "$id: sender expected after -s\n" if !$argsender;
  54.         }
  55.     } elsif ($first eq "f") {
  56.         if ($rest) {
  57.         $argfile = $rest;
  58.         } else {
  59.         $argfile = shift(@ARGV);
  60.         die "$id: file expected after -f\n" if !$argfile;
  61.         }
  62.     }
  63.     } else {
  64.     $arguser = $arg;
  65.     }
  66. }
  67.  
  68. die "$id: both user and mailfile given\n" if ($arguser && $argfile);
  69. if ($arguser) { $user = $arguser; }
  70. $mailfile = "$maildir/$user";
  71. if ($argfile) { $mailfile = $argfile; }
  72.  
  73. if (!-f $mailfile) {
  74.     die "$id: no file $mailfile\n" if ($arguser || $argfile);
  75.     die "No mail\n";
  76. }
  77. open(MBOX, $mailfile) || die "$id: unable to open $mailfile\n";
  78. while (<MBOX>) {
  79.     next unless /^From /;
  80.     ($from_from) = /^From\s(\S*)/;
  81.     ++$mailcount;
  82.     $from_user = $from_real = $subject = "";
  83.     while (<MBOX>) {
  84.     chop;
  85.     last if /^$/;
  86.     if (/^From: /) {
  87.         $from = $_;
  88.         if (/<.*>/) {
  89.         ($from_real, $from_user) = /^From:\s*\"?([^\"]*)\"?\s*<(\S*)>/;
  90.         } else {
  91.         ($from_user, $from_real) = /^From:\s*(\S*)\s*\((.*)\)/;
  92.         }
  93.     }
  94.     if (/^Subject: /) {
  95.         ($subject) = /^Subject: (.*)/;
  96.     }
  97.     }
  98.     next if $argsender && !($from =~ /$argsender/);
  99.     if ($from_real) {
  100.     $from = $from_real;
  101.     } elsif ($from_user) {
  102.     $from = $from_user;
  103.     } else {
  104.     $from = $from_from;
  105.     }
  106.     $from =~ s/^\s*(\S.*\S)\s*$/$1/;
  107.     $from = &unMime($from);
  108.     $from =~ s/\s*$//;
  109.     push(@from, $from);
  110.     if (length($from) > $longest_from) {
  111.     $longest_from = length($from);
  112.     }
  113.     if ($subject) {
  114.     $subject = &unMime($subject);
  115.     $subject =~ s/\s*$//;
  116.     push(@subject, $subject);
  117.     } else {
  118.     push(@subject, "");
  119.     }
  120. }
  121. close(MBOX);
  122. if (@from == 0) {
  123.     print "No mail\n";
  124. } else {
  125.     ++$longest_from;
  126.     while (@from) {
  127.     $from = shift(@from);
  128.     $subject = shift(@subject);
  129.     printf "%-$longest_from s \"%s\"\n", "$from:", $subject
  130.     }
  131. }
  132.  
  133.